from openai import OpenAI
from config import DEEPSEEK_API_KEY

class DeepSeekChat:
    def __init__(self):
        self.client = OpenAI(api_key=DEEPSEEK_API_KEY, base_url="https://api.deepseek.com")

    def chat_loop(self):
        print("\nDeepSeek智能助手已启动（输入'退出'结束对话）")
        while True:
            user_input = input("您：")
            if user_input.lower() == '退出':
                break
            
            try:
                response = self.client.chat.completions.create(
                    model="deepseek-chat",
                    messages=[
                        {"role": "system", "content": "You are a helpful assistant"},
                        {"role": "user", "content": user_input}
                    ],
                    stream=False
                )
                print("AI：", response.choices[0].message.content)
            except Exception as e:
                print(f"对话连接失败，错误详情: {str(e)}")
                print("请检查以下配置:\n1. config.py中的DEEPSEEK_API_KEY是否有效且已激活\n2. 网络连接是否正常\n3. API服务是否可用")
                if hasattr(e, 'response') and e.response:
                    print(f"API响应状态码: {e.response.status_code}")
                    try:
                        error_detail = e.response.json()
                        print(f"API错误详情: {error_detail}")
                    except:
                        pass
                break